<?php

class Form {
	private $_method = 'post';
	private $_name = '';
	private $_action = '';
	private $_dataTransfer = false;
	private $_formContent = '';
	
	/**
	 * Enter description here...
	 *
	 * @var Template
	 */
	private $_tpl = null;
	
	private $_requied = '';
	
	/**
	 * 
	 * Array of 
	 * 	validator_name => callback()
	 * 
	 * callback function must have 1 parameter and return true or error text
	 * @var array
	 */
	private $_validator = array();
	
	
	
	public function __construct($name, $method = 'post', $action = null) {
		$this->loadValidators();
		$this->_tpl = Template::getInstance();
		if (in_array($method, array ('get', 'post' ))) {
			$this->_method = $method;
		}
		$this->_action = $action;
		$this->_name = $name;
		
		$this->_tpl->load('form/requied');
		$this->_tpl->set('', '');
		$this->_requied = $this->_tpl->out();
	}
	
	
	
	/**
	 * Enter description here...
	 *
	 * @param unknown_type $label
	 * @param unknown_type $formParams
	 * @param unknown_type $requied
	 * @param unknown_type $validator
	 * @return Form
	 */
	public function input($label, $name, $formParams = array(), $requied = false, $validator = '') {
		$this->_tpl->load('form/input');
		$this->_tpl->set('{requied}', $requied ? $this->_requied : '');
		$this->_tpl->set('{label}', $label);
		$this->_tpl->set('{params}', $this->_paramsFromArray($formParams));
		$this->_formContent .= $this->_tpl->out();
		return $this;
	}
	
	/**
	 * Enter description here...
	 *
	 * @param unknown_type $label
	 * @param unknown_type $formParams
	 * @param unknown_type $requied
	 * @param unknown_type $validator
	 * @return Form
	 */
	public function checkbox($label, $name, $formParams = array(), $requied = false, $validator = '') {
		$this->_tpl->load('form/checkbox');
		$this->_tpl->set('{requied}', $requied ? $this->_requied : '');
		$this->_tpl->set('{label}', $label);
		$this->_tpl->set('{params}', $this->_paramsFromArray($formParams));
		$this->_formContent .= $this->_tpl->out();
		
		return $this;
	}
	
	/**
	 * Enter description here...
	 *
	 * @param unknown_type $label
	 * @param unknown_type $formParams
	 * @param unknown_type $defaultValue
	 * @param unknown_type $requied
	 * @param unknown_type $validator
	 * @return Form
	 */
	public function textarea($label, $name, $formParams = array(), $defaultValue = '', $requied = false, $validator = '') {
		$this->_tpl->load('form/textarea');
		$this->_tpl->set('{requied}', $requied ? $this->_requied : '');
		$this->_tpl->set('{label}', $label);
		$this->_tpl->set('{params}', $this->_paramsFromArray($formParams));
		$this->_tpl->set('{defaul-value}', $defaultValue);
		$this->_formContent .= $this->_tpl->out();
		
		return $this;
	}
	
	/**
	 * Enter description here...
	 *
	 * @param unknown_type $label
	 * @param unknown_type $formParams
	 * @param unknown_type $requied
	 * @param unknown_type $validator
	 * @return Form
	 */
	public function radio($label, $name, $values, $formParams = array(), $requied = false, $validator = '') {
		$radios = '';
		foreach ( $values as $key => $value ) {
			$this->_tpl->load('form/radio_item');
			$this->_tpl->set('{params}', $this->_paramsFromArray($formParams));
			$this->_tpl->set('{key}', $key);
			$this->_tpl->set('{value}', $value);
			
			$radios .= $this->_tpl->out();
		}
		
		$this->_tpl->load('form/radio');
		$this->_tpl->set('{requied}', $requied ? $this->_requied : '');
		$this->_tpl->set('{label}', $label);
		$this->_tpl->set('{radios}', $radios);
		
		$this->_formContent .= $this->_tpl->out();
		
		return $this;
	
	}
	
	/**
	 * Enter description here...
	 *
	 * @param unknown_type $label
	 * @param unknown_type $formParams
	 * @return Form
	 */
	public function submit($label, $formParams = array()) {
		$formParams['name'] = '__submit';
		$this->_tpl->load('form/submit');
		
		$this->_tpl->set('{label}', $label);
		$this->_tpl->set('{params}', $this->_paramsFromArray($formParams));
		$this->_formContent .= $this->_tpl->out();
		return $this;
	}
	
	/**
	 * Enter description here...
	 *
	 * @param unknown_type $label
	 * @param unknown_type $values
	 * @param unknown_type $formParams
	 * @param unknown_type $requied
	 * @param unknown_type $validator
	 * @return Form
	 */
	public function select($label, $name, $values, $formParams = array(), $requied = false, $validator = '') {
		$select = '';
		foreach ( $values as $key => $value ) {
			$this->_tpl->load('form/select_item');
			
			$this->_tpl->set('{key}', $key);
			$this->_tpl->set('{value}', $value);
			
			$select .= $this->_tpl->out();
		}
		
		$this->_tpl->load('form/select');
		$this->_tpl->set('{requied}', $requied ? $this->_requied : '');
		$this->_tpl->set('{label}', $label);
		$this->_tpl->set('{select}', $select);
		$this->_tpl->set('{params}', $this->_paramsFromArray($formParams));
		
		$this->_formContent .= $this->_tpl->out();
		
		return $this;
	}
	
	/**
	 * Enter description here...
	 *
	 * @param unknown_type $label
	 * @param unknown_type $name
	 * @param unknown_type $values
	 * @param unknown_type $formParams
	 * @param unknown_type $requied
	 * @param unknown_type $validator
	 * @return Form
	 */
	public function multiSelect($label, $name, $values, $formParams = array(), $requied = false, $validator = '') {
		$formParams['multiple'] = 'multiple';
		$this->select($label, $values, $formParams, $requied, $validator);
		return $this;
	}
	
	/**
	 * Enter description here...
	 *
	 * @param unknown_type $label
	 * @param unknown_type $name
	 * @param unknown_type $formParams
	 * @param unknown_type $requied
	 * @return Form
	 */
	public function file($label, $name, $formParams = array(), $requied = false) {
		$this->_dataTransfer = true;
		
		$this->_tpl->load('form/file');
		$this->_tpl->set('{requied}', $requied ? $this->_requied : '');
		$this->_tpl->set('{label}', $label);
		$this->_tpl->set('{params}', $this->_paramsFromArray($formParams));
		$this->_formContent .= $this->_tpl->out();
		
		return $this;
	}
	
	public function out() {
		$tpl = Template::getInstance();
		$tpl->load('form/form');
		
		$tpl->set('{method}', $this->_method);
		$tpl->set('{action}', $this->_action);
		$tpl->set('{enctype}', ($this->_dataTransfer) ? ' enctype="multipart/form-data"' : '');
		$tpl->set('{form}', $this->_formContent);
		$tpl->set('{name}', $this->_name);
		
		return $tpl->out();
	}
	
	public function formFromArray() {
	
	}
	
	private function _paramsFromArray($params) {
		$str = '';
		foreach ( $params as $param => $value ) {
			$str .= ' ' . $param . '="' . htmlspecialchars($value) . '"';
		}
		return $str;
	}
	
	public function isSubmit() {
		if ($this->_method == 'post') {
			if (isset($_POST['__submit'])) {
				return true;
			}
		} else {
			if (isset($_GET['__submit'])) {
				return true;
			}
		}
		return false;
	}
	
	public function isError() {
	
	}
	
	public function getErrors($asArray = true) {
	
	}
	
	public function addValidator($validatorName, $callback){
		$this->_validator[$validatorName] = $callback;
	}
	
	public function loadValidators(){
		$this->_validator['requied'] = 'validator_isEmpty';
		
	}
}